﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.ClientDW.Entities;
using MongoDB.Driver;

namespace Sustainalytics.Portfolios.BL
{

    public class CorporateDataUniversesLoader
    {
        private readonly IMongoCollection<CorporateData> _mongoCollection;

        public CorporateDataUniversesLoader(IMongoCollection<CorporateData> mongoCollection)
        {
            _mongoCollection = mongoCollection;
        }

        public virtual void SetCompaniesUniversesDictionary(ICollection<int> capitalIqIds)
        {
            var universesList = _mongoCollection.Find(x => capitalIqIds.Contains(x.CapitalIqId)).Project(x => new { x.CapitalIqId, universes = x.UniverseIds }).ToList();
            universeDictionary = universesList.ToDictionary(x => x.CapitalIqId, x => x.universes);
        }

        public virtual Dictionary<int, List<int>> GetCompaniesUniversesDictionary(ICollection<int> capitalIqIds)
        {
            var universesList = _mongoCollection.Find(x => capitalIqIds.Contains(x.CapitalIqId)).Project(x => new { x.CapitalIqId, universes = x.UniverseIds }).ToList();
            return universesList.ToDictionary(x => x.CapitalIqId, x => x.universes);
        }

        private Dictionary<int, List<int>> universeDictionary;

        public virtual ICollection<int> GetCompanyUniverses(int capitalIqId)
        {
            return universeDictionary.ContainsKey(capitalIqId) ? universeDictionary[capitalIqId] : new List<int>();
        }
    }
}